package com.dongqi.deviceData.service.impl;

import com.dongqi.deviceData.dto.*;
import com.dongqi.deviceData.entity.*;
import com.dongqi.deviceData.mapper.*;
import com.dongqi.deviceData.position.dto.PositionDTO;
import com.dongqi.deviceData.service.DeviceDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceVehicleMapper deviceVehicleMapper;

    @Autowired
    private DeviceHydPressureMapper deviceHydPressureMapper;

    @Autowired
    private DeviceGuardrailBoardMapper deviceGuardrailBoardMapper;

    @Autowired
    private DeviceMeasureMapper deviceMeasureMapper;

    @Autowired
    private DevicePositionMapper devicePositionMapper;

    @Value("${videoUrl.vihicleVideoUrl}")
    private String vihicleVideoUrl;

    @Value("${videoUrl.hydPressureVideoUrl}")
    private String hydPressureVideoUrl;

    @Value("${videoUrl.guardrailBoardVideoUrl}")
    private String guardrailBoardVideoUrl;

    @Value("${videoUrl.measureVideoUrl}")
    private String measureVideoUrl;


    @Override
    public DeviceDataDTO queryAll() {

        DeviceDataDTO deviceDataDTO = new DeviceDataDTO();

        //车辆集合
        List<DeviceVehicleEntity> deviceVehicleEntities = deviceVehicleMapper.selectList(null);
        if (deviceVehicleEntities.size() > 0) {
            List<DeviceVehicleDTO> vehicleList = new ArrayList<>();
            for (DeviceVehicleEntity deviceVehicleEntity : deviceVehicleEntities) {
                DeviceVehicleDTO deviceVehicleDTO = new DeviceVehicleDTO();
                BeanUtils.copyProperties(deviceVehicleEntity, deviceVehicleDTO);
                List<Object> positionResult = getPositionResult(deviceVehicleEntity.getDeviceNo());
                deviceVehicleDTO.setTrajectory(positionResult);
                deviceVehicleDTO.setVideoUrl(vihicleVideoUrl);
                vehicleList.add(deviceVehicleDTO);
                deviceDataDTO.setDeviceVehicleDTOList(vehicleList);
            }
        }

        //液压集合
        List<DeviceHydPressureEntity> deviceHydPressureEntities = deviceHydPressureMapper.selectList(null);
        if (deviceHydPressureEntities.size() > 0) {
            List<DeviceHydPressureDTO> hydPressureList = new ArrayList<>();
            for (DeviceHydPressureEntity hydPressureEntity : deviceHydPressureEntities) {
                DeviceHydPressureDTO hydPressureDTO = new DeviceHydPressureDTO();
                BeanUtils.copyProperties(hydPressureEntity, hydPressureDTO);
                List<Object> positionResult = getPositionResult(hydPressureEntity.getDeviceNo());
                hydPressureDTO.setTrajectory(positionResult);
                hydPressureDTO.setVideoUrl(hydPressureVideoUrl);
                hydPressureList.add(hydPressureDTO);
                deviceDataDTO.setDeviceHydPressureDTOList(hydPressureList);
            }
        }

        //护拦板集合
        List<DeviceGuardrailBoardEntity> deviceGuardrailBoardEntities = deviceGuardrailBoardMapper.selectList(null);
        if (deviceGuardrailBoardEntities.size() > 0) {
            List<DeviceGuardrailBoardDTO> guardrailBoardList = new ArrayList<>();
            for (DeviceGuardrailBoardEntity guardrailBoardEntity : deviceGuardrailBoardEntities) {
                DeviceGuardrailBoardDTO guardrailBoardDTO = new DeviceGuardrailBoardDTO();
                BeanUtils.copyProperties(guardrailBoardEntity, guardrailBoardDTO);
                List<Object> positionResult = getPositionResult(guardrailBoardEntity.getDeviceNo());
                guardrailBoardDTO.setTrajectory(positionResult);
                guardrailBoardDTO.setVideoUrl(guardrailBoardVideoUrl);
                guardrailBoardList.add(guardrailBoardDTO);
                deviceDataDTO.setDeviceGuardrailBoardDTOList(guardrailBoardList);
            }
        }


        //测量设备集合
        List<DeviceMeasureEntity> deviceMeasureEntities = deviceMeasureMapper.selectList(null);
        if (deviceMeasureEntities.size() > 0) {
            List<DeviceMeasureDTO> measureDTOList = new ArrayList<>();
            for (DeviceMeasureEntity measureEntity : deviceMeasureEntities) {
                DeviceMeasureDTO deviceMeasureDTO = new DeviceMeasureDTO();
                BeanUtils.copyProperties(measureEntity, deviceMeasureDTO);
                measureDTOList.add(deviceMeasureDTO);
                List<Object> positionResult = getPositionResult(measureEntity.getDeviceNo());
                deviceMeasureDTO.setTrajectory(positionResult);
                deviceMeasureDTO.setVideoUrl(measureVideoUrl);
                deviceDataDTO.setDeviceMeasureDTOList(measureDTOList);
            }
        }

        return deviceDataDTO;
    }


    private List<Object> getPositionResult(String deviceNo) {
        List<DevicePositionEntity> devicePositionEntities = devicePositionMapper.queryByDeviceNo(deviceNo);
        if (devicePositionEntities.size() > 0) {
            List<Object> dtoList = getPositonDTO(devicePositionEntities);
            return dtoList;
        }
        ////根据设备唯一标识查询位置表
        //List<DevicePositionEntity> devicePositionEntities = devicePositionMapper.queryByDeviceNo(deviceNo);
        //if (devicePositionEntities.size() > 0 ) {
        //    if (null != vihicleDTO) {
        //        List<Object> dtoList = getPositonDTO(devicePositionEntities);
        //        HashMap<Object, Object> map = new HashMap<>();
        //        map.put("deviceInfo", vihicleDTO);
        //        if (dtoList.size()>0){
        //            map.put("trajectoryInfo", dtoList);
        //        }
        //        list.add(map);
        //    }
        //    if (null != measureDTO) {
        //        List<Object> dtoList = getPositonDTO(devicePositionEntities);
        //        HashMap<Object, Object> map = new HashMap<>();
        //        map.put("deviceInfo", measureDTO);
        //        if (dtoList.size()>0){
        //            map.put("trajectoryInfo", dtoList);
        //        }
        //        list.add(map);
        //    }
        //    if (null != pressureDTO) {
        //        List<Object> dtoList = getPositonDTO(devicePositionEntities);
        //        HashMap<Object, Object> map = new HashMap<>();
        //        map.put("deviceInfo", pressureDTO);
        //        if (dtoList.size()>0){
        //            map.put("trajectoryInfo", dtoList);
        //        }
        //        list.add(map);
        //
        //    }
        //    if (null != guardrailBoardDTO) {
        //        List<Object> dtoList = getPositonDTO(devicePositionEntities);
        //        HashMap<Object, Object> map = new HashMap<>();
        //        map.put("deviceInfo", guardrailBoardDTO);
        //        if (dtoList.size()>0){
        //            map.put("trajectoryInfo", dtoList);
        //        }
        //        list.add(map);
        //    }
        //
        //} else {
        //    if (null != vihicleDTO) {
        //        HashMap<Object, Object> map = new HashMap<>();
        //        map.put("deviceInfo", vihicleDTO);
        //        list.add(map);
        //
        //    }
        //    if (null != guardrailBoardDTO) {
        //        HashMap<Object, Object> map = new HashMap<>();
        //        map.put("deviceInfo", guardrailBoardDTO);
        //        list.add(map);
        //
        //    }
        //    if (null != pressureDTO) {
        //        HashMap<Object, Object> map = new HashMap<>();
        //        map.put("deviceInfo", pressureDTO);
        //        list.add(map);
        //
        //    }
        //    if (null != measureDTO) {
        //        HashMap<Object, Object> map = new HashMap<>();
        //        map.put("deviceInfo", measureDTO);
        //        list.add(map);
        //    }
        //}
        //return list;
        return null;
}


    public List<Object> getPositonDTO(List<DevicePositionEntity> entities) {
        ArrayList<Object> returnList = new ArrayList<>();
        if (entities.size() > 0 && entities.size() == 1) {
            PositionDTO dto = new PositionDTO();
            BeanUtils.copyProperties(entities.get(0), dto);
            returnList.add(dto);
        } else if (entities.size() > 1) {
            for (DevicePositionEntity entity : entities) {
                PositionDTO dto = new PositionDTO();
                BeanUtils.copyProperties(entity, dto);
                returnList.add(dto);
            }
        }
        return returnList;
    }

}
